﻿using System.Windows.Controls;
using System.Windows.Input;
using MainGameModule.ViewModels;

namespace MainGameModule.Views
{
    public partial class ChatView : UserControl
    {
        private ChatViewModel viewModel;
        private int lastUIMessagesCount = 0;

        public ChatView( ChatViewModel viewModel )
        {
            InitializeComponent();
            this.viewModel = viewModel;
            this.DataContext = viewModel;

            this.ShowChatButton.Click += ( s, e ) =>
                {
                    if (this.ChatPanel.Visibility == System.Windows.Visibility.Collapsed)
                    {
                        this.ChatPanel.Visibility = System.Windows.Visibility.Visible;
                        ToolTipService.SetToolTip(this.ShowChatButton, "Hide chat");
                    }
                    else
                    {
                        this.ChatPanel.Visibility = System.Windows.Visibility.Collapsed;
                        ToolTipService.SetToolTip(this.ShowChatButton, "Show chat");
                    }
                };
        }

        private void MessageTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && !string.IsNullOrEmpty(this.MessageTextBox.Text))
            {
                this.viewModel.SendChatMessage(this.MessageTextBox.Text);
                this.MessageTextBox.Text = string.Empty;
            }
        }

        private void History_LayoutUpdated(object sender, System.EventArgs e)
        {
            if (this.History.Items.Count != this.lastUIMessagesCount)
            {
                this.MainScroll.ScrollToBottom();
                this.lastUIMessagesCount = this.History.Items.Count;
            }
        }
    }
}
